package com.liesheng.haylou.base;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;

import com.liesheng.haylou.R;
import com.liesheng.haylou.bluetooth.BleManager;
import com.liesheng.haylou.utils.ImmersiveStatusBarUtil;
import com.liesheng.haylou.utils.sp.SpUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.Locale;


public abstract class BaseActivity<T extends ViewDataBinding, V extends BaseVm> extends BaseFunActivity {

    public T mBinding;

    protected V mVm;

    private View head_left, layout_empty, layout_head, btn_empty;
    private ImageView head_iv_right;
    protected TextView head_middle, head_tv_right;
    private LinearLayout content;
    private String language;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (isFullScreen()) {
            //无title
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            //全屏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        super.onCreate(savedInstanceState);
        ImmersiveStatusBarUtil.initImmersiveStatusBarActivity(this);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        selectLanguage(SpUtil.getString("language", "zh"));
        setContentView();
        mVm = getViewModel();
        setStatusBarColor(R.color.main_bg,false);
        initData();
        if (isRegistEventBus())
            EventBus.getDefault().register(this);
    }

    private void selectLanguage(String language) {
        //设置语言类型
        Resources resources = getResources();
        Configuration configuration = resources.getConfiguration();
        switch (language) {
            case "en":
                configuration.locale = Locale.ENGLISH;
                break;
            case "zh":
                configuration.locale = Locale.SIMPLIFIED_CHINESE;
                break;
            default:
                configuration.locale = Locale.getDefault();
                break;
        }
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, displayMetrics);

    }

    protected View view;

    private void setContentView() {
        view = getLayoutInflater().inflate(R.layout.activity_base, null);
        content = view.findViewById(R.id.content);
        mBinding = getmBinding();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        content.addView(mBinding.getRoot(), params);
        setContentView(view);

        head_left = view.findViewById(R.id.head_left);
        head_middle = view.findViewById(R.id.head_middle);
        head_tv_right = view.findViewById(R.id.head_tv_right);
        head_iv_right = view.findViewById(R.id.head_iv_right);
        layout_empty = view.findViewById(R.id.layout_empty);
        layout_head = view.findViewById(R.id.layout_head);
        btn_empty = view.findViewById(R.id.btn_empty);
        if (isMarginTop() && !isFullScreen() && ImmersiveStatusBarUtil.supportedImmersiveStatusBar()) {
            FrameLayout.LayoutParams params1 = (FrameLayout.LayoutParams) view.getLayoutParams();
            params1.topMargin = ImmersiveStatusBarUtil.getStatusBarHeight();
        }
        head_left.setOnClickListener(onClickListener);
        head_iv_right.setOnClickListener(onClickListener);
        head_tv_right.setOnClickListener(onClickListener);
    }

    /**
     * 是否可以使用沉浸式
     * Is immersion bar enabled boolean.
     *
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    }

    protected abstract T getmBinding();

    protected abstract V getViewModel();

    protected abstract void initData();

    protected boolean isRegistEventBus() {
        return false;
    }

    public void setTitle(String title) {
        head_middle.setText(title);
    }

    public void setTitle(int title) {
        head_middle.setText(title);
    }

    //设置手机状态栏的颜色
    //barTextWhite : 是否要将状态栏文字颜色设为白色
    protected void setStatusBarColor(int colorId, boolean barTextWhite) {
        if (barTextWhite) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
        }

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            getWindow().setStatusBarColor(getResources().getColor(colorId));
        } else {
            getWindow().setStatusBarColor(Color.BLACK);
        }
        layout_head.setBackgroundColor(getResources().getColor(colorId));
    }

    //控制空白提示页面显示
    protected void showEmptyLayout(boolean isShow) {
        layout_empty.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    protected boolean isFullScreen() {
        return false;
    }
    protected boolean isMarginTop() {
        return true;
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.head_left:
                    onClickLeft();
                    break;
                case R.id.head_iv_right:
                case R.id.head_tv_right:
                    onClickRight();
                    break;
                case R.id.btn_empty:
                    onClickBtn();
                    break;
            }
        }
    };

    //标题栏左侧按钮点击事件
    protected void onClickLeft() {
        finish();
    }

    protected void onClickRight() {

    }

    protected void setHeadRight(String text, int ivId) {
        if (ivId == -1) {
            head_tv_right.setText("" + text);
            head_tv_right.setVisibility(View.VISIBLE);
            head_iv_right.setVisibility(View.GONE);
        } else {
            head_tv_right.setVisibility(View.GONE);
            head_iv_right.setVisibility(View.VISIBLE);
            head_iv_right.setImageResource(ivId);
        }
    }

    protected void onClickBtn() {

    }

    //控制标题栏部分是否显示
    protected void showHeadLayout(boolean isShow) {
        layout_head.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public abstract void onClickView(View view);


    @Override
    protected void onResume() {
        super.onResume();
        if (mVm != null) {
            mVm.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mVm != null) {
            mVm.onPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mVm != null) {
            mVm.onDestroy();
        }
        unsubscribe();
        EventBus.getDefault().unregister(this);
    }


    protected final int BLUETOOTH_REQUESCODE = 112;

    public boolean isBluetoothOpen() {
        boolean btOpen = BleManager.getInstance().isBluetoothOpen();
        if (!btOpen) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, BLUETOOTH_REQUESCODE);
        }

        return btOpen;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case BLUETOOTH_REQUESCODE:
                openBluetooth(resultCode == RESULT_OK);
                break;
        }
    }

    protected void openBluetooth(boolean isOpen) {
        showToast(!isOpen?"蓝牙关闭":"蓝牙开启");
    }
}
